﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
// Copyright (C) 2021 kingerrobot
// 版权所有。 
//
// 文件名：BProdMaterial
// 文件功能描述：
//
// 创建者：名字 (zzl)
// 时间：2021/12/9 8:33:13
//
// 修改人：
// 时间：
// 修改说明：
// 版本：V1.0.0
//----------------------------------------------------------------*/
#endregion

using System.Collections.Generic;
using System.Linq;

namespace KingerRobot.MexicoSailunWMS.SpiService;

internal class BBakWcsTask : IBakWcsTask
{
    public List<POBakWcsTask> GetBakWcsTasksByStartEquip(long equipId)
    {
        var db = Db.Default;
        var result = db.BakWcsTasks
            .Where(t => t.StartEquip == equipId).AsNoTracking().ToList();
        return result;
    }
    public List<POBakWcsTask> GetBakWcsTasksByDate(DateTime startTime, DateTime endTime)
    {
        var db = Db.Default;
        var result = db.BakWcsTasks
            .Where(t => t.CreateTime >= startTime && t.CreateTime <= endTime).AsNoTracking().ToList();
        return result;
    }

    public List<OutWorkCount> GetInWareHouseTaskBean(WareHouseTaskParm parm)
    {
        var db = Db.Default;
        InWareHouseTaskListBean result = new();
        var data = db.WcsEquipLocHolds.Join(db.RcsEquips, f => f.EquipId, s => s.Id, (f, s) => new
        {
            f.LocId,
            s.EquipName,
        })
         .Join(db.BakWcsTasks, f => f.LocId, s => s.TargetLoc, (f, s) => new
         {
             s.SkuId,
             s.TargetLoc,
             f.EquipName,
             s.CreateTime,
             s.TaskStatus,
         }).Where(t => t.CreateTime >= parm.StartTime && t.CreateTime <= parm.EndTime && t.TaskStatus == (int)EnumTaskStatus.Finish)
         .Join(db.ProdSkus, f => f.SkuId, s => s.Id, (f, s) => new
         {
             f.SkuId,
             f.EquipName,
             s.MaterialId,
         }).Join(db.ProdMaterials, f => f.MaterialId, s => s.Id, (f, s) => new
         {
             f.SkuId,
             f.EquipName,
             s.Spec,
         }).ToList();

        if (parm is not null && !String.IsNullOrWhiteSpace(parm.CrnName))
        {
            data = data.Where(t => t.EquipName == parm.CrnName).ToList();
        }
        if (parm is not null && !String.IsNullOrWhiteSpace(parm.Spec))
        {
            data = data.Where(t => t.Spec == parm.Spec).ToList();
        }

        var groupList = data.GroupBy(t => new { t.Spec }).Select(t => new OutWorkCount
        {
            Spec = t.Key.Spec,
            Count = t.Count()
        }).OrderByDescending(t => t.Count).ToList();

        
        return groupList;
    }

    public List<OutWorkCount> GetOutWareHouseTaskBean(WareHouseTaskParm parm)
    {
        var db = Db.Default;
        //OutWareHouseTaskListBean result = new();
        var data = db.WcsEquipLocHolds.Join(db.RcsEquips, f => f.EquipId, s => s.Id, (f, s) => new
        {
            f.LocId,
            s.EquipName,
        })
         .Join(db.BakWcsTasks, f => f.LocId, s => s.StartLoc, (f, s) => new
         {
             s.SkuId,
             s.StartLoc,
             f.EquipName,
             s.CreateTime,
             s.TaskStatus,
             s.TaskType,
         }).Where(t => t.CreateTime >= parm.StartTime && t.CreateTime <= parm.EndTime && t.TaskStatus == (int)EnumTaskStatus.Finish && (t.TaskType == (int)EnumTaskType.EMS || t.TaskType == (int)EnumTaskType.Abnormal))
           .Join(db.ProdSkus, f => f.SkuId, s => s.Id, (f, s) => new
           {
               f.SkuId,
               f.EquipName,
               s.MaterialId,
           }).Join(db.ProdMaterials, f => f.MaterialId, s => s.Id, (f, s) => new
           {
               f.SkuId,
               f.EquipName,
               s.Spec,
           }).ToList();

        if (parm is not null && !String.IsNullOrWhiteSpace(parm.CrnName))
        {
            data = data.Where(t => t.EquipName == parm.CrnName).ToList();
        }
        if (parm is not null && !String.IsNullOrWhiteSpace(parm.Spec))
        {
            data = data.Where(t => t.Spec == parm.Spec).ToList();
        }

        var groupList = data.GroupBy(t => new { t.Spec }).Select(t => new OutWorkCount
        {
            Spec = t.Key.Spec,
            Count = t.Count()
        }).OrderByDescending(t => t.Count).ToList();

        return groupList;
    }

    public List<BakTaskSkuBean> GetBakWcsTasksByDateAndType(DateTime startTime, DateTime endTime, int type)
    {
        var db = Db.Default;
        var sql = db.BakWcsTasks
            .Where(t => t.CreateTime >= startTime && t.CreateTime <= endTime && t.TaskType == type && t.TaskStatus == (int)EnumTaskStatus.Finish)
            .Select(t => new BakTaskSkuBean
            {
                SkuId = t.SkuId ?? 0,
                TargetLoc = t.TargetLoc ?? 0,
            });
        //WriteLog($"入库备份任务打印sql-{sql.ToQueryString()}", true);

        var result = sql.ToList();

        return result;
    }
    public POBakWcsTask GetBakTaskBySkuIdAndType(long skuId, int type)
    {
        var db = Db.Default;
        var result = db.BakWcsTasks
            .Where(t => t.SkuId == skuId && t.TaskType == type)
            .FirstOrDefault();
        return result;
    }

    public POBakWcsTask GetBakTasksBySummaryId(long summaryId)
    {
        var db = Db.Default;
        var result = db.BakWcsTasks
            .Where(t => t.SummaryId == summaryId)
            .FirstOrDefault();
        return result;
    }

    public List<MaterialGroupBean> GetMaterialGroupByDateAndType(int type, DateTime startTime, DateTime endTime)
    {
        throw new NotImplementedException();
    }


    public List<AbnormalBean> GetAbnormalTask(string barCode)
    {
        var db = Db.Default;

        var result = new List<AbnormalBean>();

        //传入条码为空 查询24小时前的所有数据
        if (String.IsNullOrWhiteSpace(barCode))
        {
            var oldTime = DateTime.Now.AddDays(-1);
            result = db.BakWcsTasks.Join(db.ProdSkus, f => f.SkuId, s => s.Id, (f, s) => new AbnormalBean
            {
                AbnormalRemark = f.RemarkMemo,
                BarCode = s.SkuCode,
                CreateTime = f.CreateTime,
                TaskType = f.TaskType ?? 0
            }).Where(t => t.TaskType == 9 && t.CreateTime >= oldTime).OrderByDescending(t => t.CreateTime).ToList();
        }
        else
        {
            var abnormal = db.BakWcsTasks.Join(db.ProdSkus, f => f.SkuId, s => s.Id, (f, s) => new AbnormalBean
            {
                AbnormalRemark = f.RemarkMemo,
                BarCode = s.SkuCode,
                CreateTime = f.CreateTime,
                TaskType = f.TaskType ?? 0
            }).Where(t => t.TaskType == 9 && t.BarCode == barCode).OrderByDescending(t => t.CreateTime).FirstOrDefault();
            if (abnormal is not null)
            {
                result.Add(abnormal);
            }

        }
        return result;
    }

}
